home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 076-100 / disk_091 / adlrun / adlspec.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  8KB  |  387 lines

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3.  
  4. #include "adltypes.h"
  5. #include "adlprog.h"
  6. #include "builtins.h"
  7. #include "vstring.h"
  8. #include "adlrun.h"
  9.  
  10.  
  11. extern
  12. char    *H_STR;
  13.  
  14.  
  15. special()
  16. {
  17.     int16
  18.     t;
  19.  
  20.     assertargs( "$spec", 1 );    /* Have to have at least ($spec F) */
  21.     switch( ARG( 1 ) ) {
  22.     case 1 :
  23.         debug = !debug;
  24.         break;
  25.     case 2 :
  26.         restart();
  27.         /* NOTREACHED */
  28.     case 3 :
  29.         head_term();
  30.         exit( 0 );        /* terminate game */
  31.     case 4 :
  32.         assertargs( "$spec 4", 2 );
  33.         savegame( virtstr( ARG( 2 ) ) );
  34.         break;
  35.     case 5 :
  36.         assertargs( "$spec 5", 2 );
  37.         if( restoregame( virtstr( ARG( 2 ) ) ) )
  38.         return;    /* Skip the fixup stuff */
  39.         break;
  40.     case 6 :
  41.         callextern();
  42.         break;
  43.     case 7 :
  44.         do_wordfile();
  45.         break;
  46.     case 8 :
  47.         scripter();
  48.         break;
  49.     case 9 :
  50.         write_head();
  51.         break;
  52.     case 10 :
  53.         assertargs( "$spec 10", 2 );
  54.         scrwidth = ARG( 2 );
  55.         break;
  56.     case 11 :
  57.         set_tty();
  58.         break;
  59.     case 12 :
  60.         change_tty();
  61.         break;
  62.     default :
  63.         error( 24 );    /* Illegal parameter for $spec */
  64.     }
  65.     popip();
  66.     t = pop();
  67.     sp = bp + 1;
  68.     bp = t;
  69. }
  70.  
  71. restart()
  72. {
  73.     int
  74.     i;
  75.  
  76. #if MULTIPLEX
  77.     for( i = 0; i < numact; i++ ) {
  78.     if( actlist[ i ].ttyfile != (FILE *)0 ) {
  79.         /* Close the tty */
  80.         fclose( actlist[ i ].ttyfile );
  81.         *actlist[ i ].ttyname = '\0';
  82.     }
  83.     }
  84. #endif
  85.     restarted = 1;
  86.     DO_EXIT( 4 );
  87. }
  88.  
  89.  
  90. set_tty()
  91. {
  92. #if MULTIPLEX
  93.     int
  94.     i;
  95.  
  96.     /* Set the actor's TTY */
  97.     assertargs( "$spec 11", 3 );
  98.     if( (ARG( 1 ) < 0) || (ARG( 1 ) > NUMOBJ) )
  99.     error( 31 );        /* Illegal actor for $spec 11 */
  100.     for( i = 0; i < numact; i++ ) {
  101.     if( actlist[ i ].actor == ARG( 1 ) ) {
  102.         /* We found the actor */
  103.         if( actlist[ i ].ttyfile != (FILE *)0 )
  104.         /* Close the old tty */
  105.         fclose( actlist[ i ].ttyfile );
  106.         actlist[ i ].ttyfile = fopen( virstr( ARG( 2 ) ), "r+" );
  107.         if( actlist[ i ].ttyfile == (FILE *)0 )
  108.         error( 32 );    /* Bad tty name */
  109.         strcpy( actlist[ i ].ttyname, virtstr( ARG( 2 ) ) );
  110.         return;
  111.     }
  112.     }
  113.     error( 31 );        /* Illegal actor for $spec 11 */
  114. #endif
  115. }
  116.  
  117.  
  118. change_tty()
  119. {
  120. #if MULTIPLEX
  121.     int
  122.     i;
  123.  
  124.     /* Set the current TTY */
  125.     assertargs( "$spec 12", 2 );
  126.     if( (ARG( 1 ) < 0) || (ARG( 1 ) > NUMOBJ) )
  127.     error( 33 );        /* Illegal actor for $spec 11 */
  128.     for( i = 0; i < numact; i++ ) {
  129.     if( actlist[ i ].actor == ARG( 1 ) ) {
  130.         /* We found the actor */
  131.         if( actlist[ i ].ttyfile == (FILE *)0 )
  132.         CURRTTY = SYSTTY;
  133.         else
  134.         CURRTTY = actlist[ i ].ttyfile;
  135.         return;
  136.     }
  137.     }
  138.     error( 33 );        /* Illegal actor for $spec 12 */
  139. #endif
  140. }
  141.  
  142.  
  143. invert( s, n )
  144. char
  145.     *s;
  146. int16
  147.     n;
  148. {
  149.     int16
  150.     i;
  151.  
  152.     for( i = 0; i < n; i++ )
  153.     s[ i ] ^= CODE_CHAR;
  154. }
  155.  
  156.  
  157. savegame( savename )
  158. char
  159.     *savename;
  160. {
  161.     int
  162.     savefile,
  163.     cmask;
  164.     char
  165.     tempstr[ 80 ];
  166.     int16
  167.     yesno(),
  168.     temp,
  169.     i;
  170.     struct macro
  171.     *m;
  172.  
  173.     RETVAL = 0;
  174.     savefile = open( savename, RB );
  175.     if( savefile >= 0 ) {
  176.     close( savefile );
  177.     sayer( "File " );
  178.     sayer( savename );
  179.     sayer( " already exists. Destroy it? " );
  180.     if( !yesno() )
  181.         return;
  182.     }
  183.     savefile = open( savename, WB );
  184.     if( savefile < 0 ) {
  185.     sayer( "Error opening file " );
  186.     sayer( savename );
  187.     sayer( "\n" );
  188.     return;
  189.     }
  190.     invert( savec, numsave );
  191.     for( i = 0; i < numact; i++ ) {
  192.     strcpy( tempstr, actlist[ i ].linebuf );
  193.     strcpy( actlist[ i ].savebuf, tempstr );
  194.     actlist[ i ].linebuf = actlist[ i ].savebuf;
  195.     }
  196.     temp = nummacro();
  197.     write( savefile, &hdr.adlid,      sizeof( int32 ) );
  198.     write( savefile, &numsave,          sizeof( int16 ) );
  199.     write( savefile, savec,          numsave );
  200.     write( savefile, &numact,          sizeof( int16 ) );
  201.     write( savefile, actlist,    numact *  sizeof( struct actrec ) );
  202.     write( savefile, vecverb,    10 *      sizeof( int16 ) );
  203.     write( savefile, &currturn,          sizeof( int16 ) );
  204.     write( savefile, &prompter,          sizeof( int16 ) );
  205.     write( savefile, &numd,          sizeof( int16 ) );
  206.     write( savefile, demons,    10 *      sizeof( int16 ) );
  207.     write( savefile, &numf,          sizeof( int16 ) );
  208.     write( savefile, fuses,    10 *      sizeof( int16 ) );
  209.     write( savefile, ftimes,    10 *      sizeof( int16 ) );
  210.     write( savefile, f_actors,    10 *      sizeof( int16 ) );
  211.     write( savefile, objspace,    NUMOBJ  * sizeof( struct objrec ) );
  212.     write( savefile, verbspace,    NUMVERB * sizeof( struct verbrec ) );
  213.     write( savefile, stack,    NUMVAR  *  sizeof( int16 ) );
  214.     write( savefile, &temp,          sizeof( int16 ) );
  215.     for( m = mactab; m; m = m->next ) {
  216.     invert( m->name, 10 );
  217.     invert( m->val, 80 );
  218.     write( savefile, m->name, 10 );
  219.     write( savefile, m->val,  80 );
  220.     invert( m->name, 10 );
  221.     invert( m->val, 80 );
  222.     }
  223.     close( savefile );
  224. #if UNIX
  225.     cmask = umask( 0 );
  226.     (void)umask( cmask );
  227.     chmod( savename, 0666 & ~cmask );
  228. #endif
  229.     invert( savec, numsave );
  230.     RETVAL = 1;
  231. }
  232.  
  233.  
  234. restoregame( savename )
  235. char
  236.     *savename;
  237. {
  238.     int
  239.     savefile;
  240.     char
  241.     mname[ 10 ],
  242.     mval[ 80 ];
  243.     int32
  244.     tempid;
  245.     int16
  246.     num,
  247.     i;
  248.  
  249.     savefile = open( savename, RB );
  250.     if( savefile < 0 ) {
  251.     sayer( "Error opening file " );
  252.     sayer( savename );
  253.     sayer( "\n" );
  254.     return 0;
  255.     }
  256.     read( savefile, &tempid,    sizeof( int32 ) );
  257.     if( tempid != hdr.adlid ) {
  258.     sayer( "Error: \"" );
  259.     sayer( savename );
  260.     sayer( "\" is not a save file for this game.\n" );
  261.     return 0;
  262.     }
  263. #if MULTIPLEX
  264.     for( i = 0; i < numact; i++ )
  265.     if( actlist[ i ].ttyfile != (FILE *)0 )
  266.         /* Close the tty */
  267.         fclose( actlist[ i ].ttyfile );
  268. #endif
  269.     read( savefile, &numsave,         sizeof( int16 ) );
  270.     read( savefile, savec,         numsave );
  271.     read( savefile, &numact,         sizeof( int16 ) );
  272.     read( savefile, actlist,    numact * sizeof( struct actrec ) );
  273.     read( savefile, vecverb,    10 *     sizeof( int16 ) );
  274.     read( savefile, &currturn,         sizeof( int16 ) );
  275.     read( savefile, &prompter,         sizeof( int16 ) );
  276.     read( savefile, &numd,         sizeof( int16 ) );
  277.     read( savefile, demons,    10 *     sizeof( int16 ) );
  278.     read( savefile, &numf,         sizeof( int16 ) );
  279.     read( savefile, fuses,    10 *     sizeof( int16 ) );
  280.     read( savefile, ftimes,    10 *     sizeof( int16 ) );
  281.     read( savefile, f_actors,    10 *     sizeof( int16 ) );
  282.     read( savefile, objspace,    NUMOBJ * sizeof( struct objrec ) );
  283.     read( savefile, verbspace,    NUMVERB * sizeof( struct verbrec ) );
  284.     read( savefile, stack,    NUMVAR * sizeof( int16 ) );
  285.     read( savefile, &num,         sizeof( int16 ) );
  286.     clearmacro();
  287.     for( i = 0; i < num; i++ ) {
  288.     read( savefile, mname,    10 );
  289.     read( savefile, mval,    80 );
  290.     invert( mname, 10 );
  291.     invert( mval, 80 );
  292.     define( mname, mval );
  293.     }
  294.     close( savefile );
  295.  
  296.     /* Fix up the actlist string pointers, which may have changed since
  297.        the file was saved. */
  298.     for( i = 0; i < numact; i++ )
  299.     actlist[ i ].linebuf = actlist[ i ].savebuf;
  300.  
  301.     /* Decode the saved character buffer */
  302.     invert( savec, numsave );
  303.  
  304. #if MULTIPLEX
  305.     /* Reopen the actor tty's */
  306.     for( i = 0; i < numact; i++ )
  307.     if( actlist[ i ].ttyfile != (FILE *)0 )
  308.         actlist[ i ].ttyfile = fopen( actlist[ i ].ttyname, "r+" );
  309. #endif
  310.  
  311.     /* It was a successful restore, so stop running */
  312.     ip = 0L;
  313.     bp = sp = NUMVAR;
  314.     return 1;
  315. }
  316.  
  317.  
  318. do_wordfile()
  319. {
  320.     assertargs( "$spec 7", 2 );
  321.     if( wordwrite )
  322.     fclose( wordfile );
  323.     wordwrite = 0;
  324.     if( ARG( 2 ) ) {
  325.     wordwrite = 1;
  326.     wordfile = fopen( virtstr( ARG( 2 ) ), "a" );
  327.     }
  328. }
  329.  
  330.  
  331. callextern()
  332. {
  333. #if UNIX
  334.     int
  335.     i;
  336.     char
  337.     **argv,
  338.     *t;
  339.  
  340.     /* Have to have at least ($spec 6 "name" 0) */
  341.     assertargs( "$spec 6", 3 );
  342.     if( fork() )
  343.     /* We're the parent - wait for the child to die */
  344.     RETVAL = wait( 0 );
  345.     else {
  346.     /* We're the child. Get memory for the argument vector */
  347.     argv = (char **)malloc( (RETVAL - 1) * sizeof( char * ) );
  348.     if( argv == (char **)0 )
  349.         error( 27 );            /* Out of memory */
  350.  
  351.     /* Fill the argument vectors */
  352.     for( i = 2; i < RETVAL && ARG( i ); i++ ) {
  353.         t = virtstr( ARG( i ) );
  354.         argv[ i - 2 ] = malloc( strlen( t ) + 1 );
  355.         if( argv[ i - 2 ] == (char *)0 )
  356.         error( 27 );            /* Out of memory */
  357.  
  358.         strcpy( argv[ i - 2 ], t );
  359.     }
  360.  
  361.     /* Set the last argument to be zero */
  362.     argv[ RETVAL - 2 ] = (char *)0;
  363.  
  364.     /* Execute the program */
  365.     execv( argv[ 0 ], argv );
  366.  
  367.     /* In case the exec failed, exit. */
  368.     head_term();
  369.     exit( -1 );
  370.     }
  371. #endif
  372. }
  373.  
  374.  
  375.  
  376. scripter()
  377. {
  378.     assertargs( "$spec 8", 2 );
  379.     if( scriptfile != (FILE *)0 )
  380.     fclose( scriptfile );
  381.     scriptfile = (FILE *)0;
  382.     if( ARG( 2 ) )
  383.     scriptfile = fopen( virtstr( ARG( 2 ) ), "w" );
  384. }
  385.  
  386. /*** EOF adlspec.c ***/
  387.